package com.agu.config;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authz.Authorizer;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Shiro配置
 *
 */
@Configuration
public class ShiroConfig {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Bean(name = "authenticator")
	public Authenticator authenticator() {
		ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
		return authenticator;
	}

	@Bean(name = "authorizer")
	public Authorizer authorizer() {
		ModularRealmAuthorizer authenticator = new ModularRealmAuthorizer();
		return authenticator;
	}
	
    @Bean(name = "sessionManager")
    public DefaultWebSessionManager sessionManager(){
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        //设置session过期时间为1小时(单位：毫秒)，默认为30分钟
        sessionManager.setGlobalSessionTimeout(60 * 60 * 1000);
        sessionManager.setSessionValidationSchedulerEnabled(true);
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager securityManager(UserRealm userRealm, SessionManager sessionManager,Authenticator authenticator,Authorizer authorizer) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setAuthorizer(authorizer);
        securityManager.setAuthorizer(authorizer);
        securityManager.setRealm(userRealm);
        securityManager.setSessionManager(sessionManager);
        return securityManager;
    }

	@Bean(name ="shiroFilter")
	public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
		ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
		shiroFilter.setSecurityManager(securityManager);
		shiroFilter.setLoginUrl("/login.html");
		//shiroFilter.setSuccessUrl("/index.html");
		shiroFilter.setUnauthorizedUrl("/");

		Map<String, String> filterMap = new LinkedHashMap<>();
		filterMap.put("/public/**", "anon");
		filterMap.put("/webjars/**", "anon");
		filterMap.put("/api/**", "anon");
		filterMap.put("/web/**", "anon");
		filterMap.put("/sys/**", "anon");
		filterMap.put("/pmproject/**", "anon");
		filterMap.put("/css/**", "anon");
		filterMap.put("/js/**", "anon");
		filterMap.put("/img/**", "anon");
		filterMap.put("/fonts/**", "anon");

		filterMap.put("/auth/captcha.jpg", "anon");
		filterMap.put("/auth/login", "anon");
		filterMap.put("/auth/logout", "anon");
		filterMap.put("/auth/info", "anon");
		filterMap.put("/auth/menu", "anon");
		
		//filterMap.put("/index.html", "anon");

		// swagger配置
		filterMap.put("/swagger**", "anon");
		filterMap.put("/v2/api-docs", "anon");
		filterMap.put("/swagger-resources/configuration/ui", "anon");

		filterMap.put("/login.html", "anon");
		filterMap.put("/sys/login", "anon");
		filterMap.put("/captcha.jpg", "anon");
		filterMap.put("/**", "authc");
		shiroFilter.setFilterChainDefinitionMap(filterMap);

		return shiroFilter;
	}

	@Bean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
		DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
		proxyCreator.setProxyTargetClass(true);
		return proxyCreator;
	}

}
